package app.util.image.action
{
	import app.util.*;
	
	import flash.display.*;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.*;
	
	public class ThresholdAction extends AbstractAction
	{
		private var matrix:Array;
		private var thr:Number;
		
		public function setValue(v:Number):ThresholdAction
		{
			this.thr = v;
			v = v * 1.1 - 1.5;
			this.matrix =  [ColorMatrix.R_LUM*256, ColorMatrix.G_LUM*256, ColorMatrix.B_LUM*256, 0,  -256*v, 
							ColorMatrix.R_LUM*256 ,ColorMatrix.G_LUM*256, ColorMatrix.B_LUM*256, 0,  -256*v, 
							ColorMatrix.R_LUM*256, ColorMatrix.G_LUM*256, ColorMatrix.B_LUM*256, 0,  -256*v, 
							0, 0, 0, 1, 0]; 	
			return this;
		}
		
		public override function getDescription():String
		{
			return "Threshold " + this.thr.toFixed(1);
		}
		
		public override function getActionedBitmapData():BitmapData
		{
			var old:BitmapData = Mediator.getCurrentBitmapData();
			var ret:BitmapData = old.clone();
			var filter:ColorMatrixFilter = new ColorMatrixFilter(matrix);
			ret.applyFilter(ret, ret.rect, new Point(0, 0), filter);
			return ret;
		}	
	}
}